//
//  APOLAPI_Login.m
//  Apallo
//
//  Created by Jason Poh on 20/8/14.
//  Copyright (c) 2014 Apallo. All rights reserved.
//

#import "APOLAPI_Login.h"
#import "APOConstants.h"
#import "APOStringUtility.h"

@implementation APOLAPI_Login

NSString *const LAPI_LOGIN_NOTIFICATION = @"LAPI_LOGIN_NOTIFICATION";
NSString *const LAPI_VALIDATE = @"LAPI_VALIDATE";
NSString *const LAPI_USER_ID = @"LAPI_USER_ID";
NSString *const LAPI_FULL_NAME = @"LAPI_FULL_NAME";
NSString *const LAPI_EMAIL = @"LAPI_EMAIL";

+(void)validate: (NSString*)token
{
    NSLog(@"APOLAPI_Login#Validate: Validating token ...");
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString * url_str = [NSString stringWithFormat:@"https://ivle.nus.edu.sg/api/Lapi.svc/Validate?APIKey=%@&Token=%@", API_KEY, token];
    [manager GET:url_str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
       if ([responseObject isKindOfClass:[NSDictionary class]]) {
            NSDictionary *responseDict = responseObject;
            NSLog(@"APOLAPI_Login#Validate: is responseDict");
           NSString * validate_result_str = [[NSString alloc] initWithFormat:@"%@",[responseDict valueForKey:@"Success"] ];
           if([validate_result_str isEqualToString:@"1"]) {
                NSLog(@"APOLAPI_Login#Validate: Success = 1");
               [APOLAPI_Login postNotificationWithString:@"SUCCESS" :LAPI_VALIDATE];
           } else {
               NSLog(@"APOLAPI_Login#Validate: Success = 0");
           }
           
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"APOLAPI_Login#Validate Error: %@", error);
    }];
}

+(void)getUserID: (NSString*)token
{
    NSLog(@"APOLAPI_Login#getUserID: Getting userID");
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString * url_str = [NSString stringWithFormat:@"https://ivle.nus.edu.sg/api/Lapi.svc/UserID_Get?APIKey=%@&Token=%@", API_KEY, token];
    [manager GET:url_str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *response_str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        response_str = [APOStringUtility removeDoubleQuotes:response_str];
        NSLog(@"APOLAPI_Login#getUserID: userID = %@", response_str);
        [APOLAPI_Login postNotificationWithString:response_str :LAPI_USER_ID];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"APOLAPI_Login#getUserID: Error: %@", error);
    }];
}

+(void)getFullName: (NSString*)token
{
    NSLog(@"APOLAPI_Login#getFullName: Getting full name ...");
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString * url_str = [NSString stringWithFormat:@"https://ivle.nus.edu.sg/api/Lapi.svc/UserName_Get?APIKey=%@&Token=%@", API_KEY, token];
    [manager GET:url_str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *response_str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        response_str = [APOStringUtility removeDoubleQuotes:response_str];
        NSLog(@"APOLAPI_Login#getFullName: full name = %@", response_str);
        [APOLAPI_Login postNotificationWithString:response_str :LAPI_FULL_NAME];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"APOLAPI_Login#getFullName: Error: %@", error);
    }];
}

+(void)getEmail:(NSString*)token
{
    NSLog(@"APOLAPI_Login#getEmail: Getting email ...");
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString * url_str = [NSString stringWithFormat:@"https://ivle.nus.edu.sg/api/Lapi.svc/UserEmail_Get?APIKey=%@&Token=%@", API_KEY, token];
    [manager GET:url_str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *response_str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        response_str = [APOStringUtility removeDoubleQuotes:response_str];
        NSLog(@"APOLAPI_Login#getEmail: email = %@", response_str);
        [APOLAPI_Login postNotificationWithString:response_str :LAPI_EMAIL];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"APOLAPI_Login#getEmail: Error: %@", error);
    }];
}

+ (void)postNotificationWithString:(NSString *)data :(NSString*) key //post notification method and logic
{
    NSDictionary *dictionary = [NSDictionary dictionaryWithObject:data forKey:key];
    [[NSNotificationCenter defaultCenter] postNotificationName:LAPI_LOGIN_NOTIFICATION object:nil userInfo:dictionary];
}


@end
